package com.wangzhenyu.learn.test.java.io

import java.io.*

object InstanceReader {

    //FileReader
    fun test01() {
        val path = "D:\\picture\\test.txt"
        var fileReader: FileReader? = null
        var read = 0
        val buf = CharArray(8)
        try {
            fileReader = FileReader(path)

            while (read != -1) {
                read = fileReader.read(buf)
                if (read != -1) print(String(buf, 0, read))
            }

        } catch (e: Exception) {

        } finally {
            fileReader?.close()
        }
    }

    //BufferedReader
    fun test02() {
        val path = "D:\\picture\\test.txt"
        var bufferedReader: BufferedReader? = null
        var line: String? = ""

        try {
            bufferedReader = File(path).bufferedReader()
                //BufferedReader(FileReader(path))
            while (line != null) {
                line = bufferedReader.readLine()
                if (line != null) println(line)
            }
        } catch (e: Exception) {

        } finally {
            bufferedReader?.close()
        }
    }

    //拷贝
    fun test03() {
        val readerPath = "D:\\picture\\test.txt"
        val writerPath = "D:\\picture\\llf.txt"
        var bufferedReader: BufferedReader? = null
        var bufferedWriter: BufferedWriter? = null
        var line: String? = ""

        try {
            bufferedReader = BufferedReader(FileReader(readerPath))
            bufferedWriter = BufferedWriter(FileWriter(writerPath))
            while (line != null) {
                line = bufferedReader.readLine()
                if (line != null) {
                    bufferedWriter.write(line)
                    bufferedWriter.newLine()
                }
            }
        } catch (e: Exception) {

        } finally {
            bufferedReader?.close()
            bufferedWriter?.close()
        }

    }


}